<div class="layui-fluid">
  <div class="layui-row">
    <div class="layui-col-xs12">
      <div class="layui-card">
        <div class="layui-card-header">
        	<h2>
          <a href="#/layui/pageList" title="返回上一页">
            <i class="layui-icon">&#xe65c;</i>
          </a>
          <span>编辑页面</span>
          </h2>
        </div>
        <div class="layui-card-body">
          <form class="layui-form" action="" lay-filter="page_form">
            <div class="layui-form-item">
              <label class="layui-form-label">页面类型<span class="red">*</span></label>
              <div class="layui-input-block">
                <select name="PageType" class="page_type" lay-filter="pageType" lay-verify="required">
                  <option value="0">左侧菜单栏</option>
                  <option value="1">普通操作页面</option>
                </select>
              </div>
            </div>
            <div class="layui-form-item one_area">
              <label class="layui-form-label">一级目录<span class="red">*</span></label>
              <div class="layui-input-block" >
                <select name="OneID" lay-filter="oneID" class="oneID" lay-verify="required">
                  <option value="0">基础数据</option>
                  <option value="1">用户管理</option>
                  <option value="2">商脉管理</option>
                  <option value="3">产品管理</option>
                  <option value="4">官网管理</option>
                  <option value="5">系统设置</option>
                </select>
              </div>
            </div>
            <div class="layui-form-item two_area" style="display: none">
              <label class="layui-form-label">二级目录</label>
              <div class="layui-input-block">
                <select name="TwoID" lay-filter="twoID" class="two_select_area">

                </select>
              </div>
            </div>
            <div class="layui-form-item">
              <label class="layui-form-label">是否独立<span class="red">*</span></label>
              <div class="layui-input-block">
                <select name="IsIndependent" class="independent" lay-verify="required">
                  <option value="1">是</option>
                  <option value="0">否</option>
                </select>
              </div>
            </div>
            <div class="layui-form-item">
              <label class="layui-form-label">页面名称<span class="red">*</span></label>
              <div class="layui-input-block">
                <input type="text" name="Name" required lay-verify="required" autocomplete="off" class="page_name layui-input" id="user_id">
              </div>
            </div>
            <div class="layui-form-item">
              <label class="layui-form-label">页面源文件</label>
              <div class="layui-input-block">
                <input type="text" name="PagePath" required autocomplete="off" class="page_path layui-input">
              </div>
            </div>
            <div class="layui-form-item layui-form-text">
              <label class="layui-form-label">描述</label>
              <div class="layui-input-block">
                <textarea name="Description" placeholder="请输入内容" class=" page_des layui-textarea"></textarea>
              </div>
            </div>
            <div class="layui-form-item">
              <label class="layui-form-label">排序号<span class="red">*</span></label>
              <div class="layui-input-inline">
                <input type="number" name="OrderID" required lay-verify="required|number" autocomplete="off" class="page_orderID layui-input">
              </div>
            </div>
            <div class="layui-form-item">
              <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="formDemo_hash">立即提交</button>
              </div>
            </div>
          </form>
        </div>
      </div>
    </div>
  </div>
</div>

<script>
  layui.use(['form','route'], function () {

    var form = layui.form;
    var route = layui.route
    var params = route.params()
    var obj={}

    if(params!=null){
      DBHelper.Get('Admin_Xn_Page/'+params.Guid,function (res) {
        obj = res
        console.log(res)
        $('.page_name').val(res.Name)
        $('.page_path').val(res.PagePath)
        $('.page_des').val(res.Description)
        $('.page_orderID').val(res.OrderID)

        //独立
        $(".independent").find("option[value='"+res.IsIndependent+"']").prop("selected",true);
        form.render();

        //页面类型
        $('.page_type').find("option[value='"+res.PageType+"']").prop("selected",true);
        if(res.PageType == 1){
          $('.two_area').show()
        }
        form.render();
        //一级
        $('.oneID').find("option[value='"+res.OneID+"']").prop("selected",true);
        form.render();
        //一级关联二级
         if (res.OneID == 0){
        $('.two_select_area').html('<option value=""></option>\
                  <option value="0">平台设置</option>\
                  <option value="1">授权设置</option>\
                  <option value="2">分享海报</option>\
                  <option value="3">会员等级</option>')
          form.render();
        }else if(res.OneID == 1){
          $('.two_select_area').html('<option value=""></option>\
                      <option value="4">会员列表</option>\
                      <option value="5">会议列表</option>\
                      <option value="6">评论管理</option>\
                      <option value="7">职位列表</option>')
          form.render();
        }else if(res.OneID == 2){
          $('.two_select_area').html('<option value=""></option>\
                      <option value="8">分会管理</option>\
                      <option value="9">商圈管理</option>')
          form.render();
        }else if(res.OneID == 3){
          $('.two_select_area').html('<option value=""></option>\
                      <option value="10">产品列表</option>')
          form.render();
        }else if(res.OneID == 4){
          $('.two_select_area').html('<option value=""></option>\
                      <option value="11">官网管理</option>')
          form.render();
        }else if(res.OneID == 5){
          $('.two_select_area').html('<option value=""></option>\
                      <option value="12">权限管理</option>\
                      <option value="13">管理员设置</option>\
                      <option value="14">页面设置</option>\
                      <option value="15">数据库备份</option>\
                      <option value="16">操作日志</option>')
          form.render();
        }

        //二级
        $('.two_select_area').find("option[value='"+res.TwoID+"']").prop("selected",true);
        form.render();

      },function (res) {
        // body...
      })
    }else {
      //新增
    }

    form.render();

    //类型选择
    form.on('select(pageType)',function (data) {
      // form.val('page_form',{
      //   "oneID":0
      // })
      $(".oneID").find("option[value='0']").prop("selected",true);
      form.render();

      $('.two_select_area').html('<option value=""></option>\
                  <option value="0">平台设置</option>\
                  <option value="1">授权设置</option>\
                  <option value="2">分享海报</option>\
                  <option value="3">会员等级</option>')
      form.render();

      if(data.value == 1) {
        $('.two_area').show()
      }else {
        $('.two_area').hide()
      }
    })

    //一级目录选择
    form.on('select(oneID)',function (data) {
      if (data.value == 0){
        $('.two_select_area').html('<option value=""></option>\
                  <option value="0">平台设置</option>\
                  <option value="1">授权设置</option>\
                  <option value="2">分享海报</option>\
                  <option value="3">会员等级</option>')
      form.render();
    }else if(data.value == 1){
      $('.two_select_area').html('<option value=""></option>\
                      <option value="4">会员列表</option>\
                      <option value="5">会议列表</option>\
                      <option value="6">评论管理</option>\
                      <option value="7">职位列表</option>')
      form.render();
    }else if(data.value == 2){
      $('.two_select_area').html('<option value=""></option>\
                      <option value="8">分会管理</option>\
                      <option value="9">商圈管理</option>')
      form.render();
    }else if(data.value == 3){
      $('.two_select_area').html('<option value=""></option>\
                      <option value="10">产品列表</option>')
      form.render();
    }else if(data.value == 4){
      $('.two_select_area').html('<option value=""></option>\
                      <option value="11">官网管理</option>')
      form.render();
    }else if(data.value == 5){
      $('.two_select_area').html('<option value=""></option>\
                      <option value="12">权限管理</option>\
                      <option value="13">管理员设置</option>\
                      <option value="14">页面设置</option>\
                      <option value="15">数据库备份</option>\
                      <option value="16">操作日志</option>')
      form.render();
    }
    })

    //监听提交
    form.on('submit(formDemo_hash)', function (data) {
      // layer.msg(JSON.stringify(data.field));
      if(params!=null){
        obj.Name=data.field.Name,
        obj.OrderID=data.field.OrderID,
        obj.OneID=data.field.OneID,
        obj.TwoID=data.field.TwoID||-1,
        obj.ThreeID=-1,
        obj.Description=data.field.Description,
        obj.PageType=data.field.PageType,
        obj.PagePath=data.field.PagePath,
        obj.ModifyTime=getNowFormatDate(),
        obj.ModifyUser=localStorage.getItem('user'),
        obj.IsDeleted=1,
        obj.IsIndependent=data.field.IsIndependent
        DBHelper.Put('Admin_Xn_Page/'+params.Guid,obj,function (res) {
          layer.open({
              content: '修改成功',
              yes: function(index, layero){
                  location.href='#/layui/pageList'
                  layer.close(index);
              }
            })
        },function (res) {
          // body...
        })
      }else {
        var newData ={
          Name:data.field.Name,
          OrderID:data.field.OrderID,
          OneID:data.field.OneID,
          TwoID:data.field.TwoID||-1,
          ThreeID:-1,
          Description:data.field.Description,
          PageType:data.field.PageType,
          PagePath:data.field.PagePath,
          CreateTime:getNowFormatDate(),
          CreateUser:localStorage.getItem('user'),
          ModifyTime:getNowFormatDate(),
          ModifyUser:localStorage.getItem('user'),
          IsDeleted:1,
          IsIndependent:data.field.IsIndependent
        }
        // console.log(newData)
        DBHelper.Post('Admin_Xn_Page',newData,function (res) {
          if(res.Guid){
            layer.open({
              content: '新建成功',
              yes: function(index, layero){
                  location.href='#/layui/pageList'
                  layer.close(index);
              }
            })
          }
        },function (res) {
          
        })
      }
      return false;
    });

  });
</script>
<style>
</style>